package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.CreateOrder;
import com.mofang.powerdekorhelper.model.CustomerActivity;
import com.mofang.powerdekorhelper.model.CustomerDetail;
import com.mofang.powerdekorhelper.model.OrderDetial;
import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.CustomerDetialView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/24 0024.
 */

public class CustomerDetialPersenter extends BasePresent<CustomerDetialView> {

    RetrofitSerives retrofitSerives;
    Call<CustomerDetail> customerDetailCall;
    Call<CreateOrder> createOrderCall;
    Call<CustomerActivity> customerCall;
    Call<OrderDetial> orderDetialCall;

    public void getCustomerDetail(Integer customerId, Integer sellerId, Integer shopId) {

        String jsonParams = "";

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("customerId", customerId);
            jsonObject.put("sellerId", sellerId);
            jsonObject.put("shopId", shopId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
        customerDetailCall = retrofitSerives.getCustomerDetail(body);
        customerDetailCall.enqueue(new Callback<CustomerDetail>() {
            @Override
            public void onResponse(Call<CustomerDetail> call, Response<CustomerDetail> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setCustomerDetial(response.body());
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<CustomerDetail> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    // 创建订单
    public void getCreateOrder(String customerPhone, String orderCode) {

        String jsonParams = "";

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("customerPhone", customerPhone);
            jsonObject.put("orderCode", orderCode);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        L.i("ProcessPersenter", "jsonParams----------->" + jsonParams);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        createOrderCall = retrofitSerives.getCreateOrder(body);

        createOrderCall.enqueue(new Callback<CreateOrder>() {
            @Override
            public void onResponse(Call<CreateOrder> call, Response<CreateOrder> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0") || response.body().getCode().equals("2")) {
                        view.setCreateOrder(response.body());
                    }

                   /* else if (response.body().getCode().equals("2")) {
                        view.onError(response.body().getMessage());
                        view.showNullLayout();
                    } */

                    else if (response.body().getCode().equals("1")) {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<CreateOrder> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    Call<ResultMessage> closeCall;

    public void getCloseOrder(final String requestStr) {
        view.showProgress();
        final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), requestStr);
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        closeCall = retrofitSerives.getCloseOrder(body);
        closeCall.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {
                        view.setCloseOrder(response.body());
                    } else {
                        view.onError(Constants.COMMIT_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.COMMIT_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {
                view.onError(Constants.COMMIT_ERROR_INFO);
            }
        });
    }

    public void getCustomerActivity(Integer customerId, Integer shopId) {

        String jsonParams = "";

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("customerId", customerId);
            jsonObject.put("shopId", shopId);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        jsonParams = jsonObject.toString();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        L.i("ProcessPersenter", "jsonParams----------->" + jsonParams);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        customerCall = retrofitSerives.getCustomerActivity(body);

        customerCall.enqueue(new Callback<CustomerActivity>() {
            @Override
            public void onResponse(Call<CustomerActivity> call, Response<CustomerActivity> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode().equals("0")) {
                        view.setCustomerActivity(response.body());
                    } else if (response.body().getCode().equals("2")) {
                        view.onError(Constants.OFF_ORDER_ALREADY_EXIST);
                        //view.showNullLayout();
                    } else if (response.body().getCode().equals("1")) {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<CustomerActivity> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    /**
     * 订单详情
     *
     * @param jsonParams
     */
    public void getCheckOrder(String jsonParams) {

        view.showProgress();
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
        orderDetialCall = retrofitSerives.getCheckOrder(body);
        orderDetialCall.enqueue(new Callback<OrderDetial>() {
            @Override
            public void onResponse(Call<OrderDetial> call, Response<OrderDetial> response) {

                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0") || response.body().getCode().equals("2")) {
                        view.setCheckOrder(response.body());
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<OrderDetial> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    @Override
    public void stopRequest() {

    }
}
